currentInput = null
//获得传过来的login与在数据库中对应的表单
var paras = location.search;            //search获得地址中的参数，内容为'?itemId=12'
var result = paras.match(/[^\?&]*=[^&]*/g);     //match是字符串中符合的字段一个一个取出来，result中的值为['login=xx','table=admin']
paras = {};                    //让paras变成没有内容的json对象
for(i in result){
    var temp = result[i].split('=');    //split()将一个字符串分解成一个数组,两次遍历result中的值分别为['itemId','xx']
    paras[temp[0]] = temp[1];
}
var content = decodeURI(paras.content); //根据参数名"itemId"，获取参数值
console.log(content)

function clickQuestion(event) {
    if (currentInput != null) {
        myContent = $($(event).children()[1]).html()
        $(currentInput).val(myContent)
    }
}

function clickInput(event) {
    currentInput = event
}

function deleteSubQuestion(obj) {
    $(obj).parent().remove()
    refreshIndex()
}
function refreshIndex() {
    size = $("#input_questions").children().length
    for (i = 0; i < size; i++) {
        currentQuestion = $("#input_questions").children()[i]
        currentSpan = $(currentQuestion).children()[0]
        $(currentSpan).html(`子问题${i + 1}：`)
    }
}
$(document).ready(function(){
    $("#origin_question").html(content)
});
$(document).ready(function(){
    $("#decompose").click(function(){
        var json = {}
        var quesArray = []
        count = $("#input_questions").children().length
        for (i = 0; i < count; i++) {
            question = $($($("#input_questions").children()[i]).children('input')[0]).val()
            quesArray.push(question)

        }
        json['questions'] = quesArray
        console.log(content)
        json['origin_question'] = content
        if (quesArray.length <= 0) {
            alert('请在分解问题列表中选择若干问题')
            return
        }
        window.location.href="SearchResult.html?" + "content=" + encodeURI(JSON.stringify(json))
    });
});
$(document).ready(function(){
    $("#direct").click(function(){
        if (content == null || content.length <= 0) {
            alert('当前没有问题')
            return
        }
        var json = {}
        var quesArray = [content]
        json['questions'] = quesArray
        console.log(content)
        json['origin_question'] = content
        if (quesArray.length <= 0) {
            alert('请在分解问题列表中选择若干问题')
            return
        }
        window.location.href="SearchResult.html?" + "content=" + encodeURI(JSON.stringify(json));
    });
});

$(document).ready(function(){
    $(".add_question").click(function(){
        count = $("#input_questions").children().length;
        console.log(count)
        $("#input_questions").append(`<div class="sub_question"><span class="title">子问题${count + 1}：</span><input onclick="clickInput(this)"><div onclick="deleteSubQuestion(this)" class="delete"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span><span class="end">删除</span></div></div>`)
    });
});

http_question_extend()

function http_question_extend() {
    var jsonStr = getJsonData()
    console.log(jsonStr)
    $.ajax({
         type: "POST",
         url: "http://172.18.31.218:9991/questionExtend",
         contentType: "application/json; charset=utf-8",
         data: jsonStr,
         dataType: "json",
         success: function (ret) {
            hideLoading()
            console.log("问题分解结果 : " + JSON.stringify(ret))
            if(ret.code == 200) {
                var questionList = ret.data.extendItemList

                questionList.sort(function(a, b){
                    if (a.score >= b.score) {
                        return -1
                    } else {
                        return 1
                    }
                })
                console.log(questionList.length)
                for (i = 0; i < questionList.length; i++) {
                    $('#questions').append(`<button onclick="clickQuestion(this)" type="button" class="list-group-item item"><span>${i + 1}</span>.<span>${questionList[i].topic}</span></button>`)
                }
            } else {
                alert(ret.message)
            }
             
         },
         error: function (err) {
            hideLoading()
             alert('网络请求失败')
         }
    })
}

function getJsonData() {
    var json = {
        'extendText':content,
    }
    return JSON.stringify(json)
}

function showLoading() {
    $('.loading').show()
}

function hideLoading() {
    $('.loading').hide()
}